#include "PlatformIeeeEui64.h"

module LocalIeeeEui64P {
  provides {
    interface LocalIeeeEui64;
  }
  uses {
    interface ReadId48;
  }
}

implementation {
  ieee_eui64_t eui = {{0x00}};

  bool have_id = FALSE;

  command ieee_eui64_t LocalIeeeEui64.getId () {
    uint8_t buf[6] = {0};
    error_t e;

    if (!have_id) {
      e = call ReadId48.read(buf);
      if (e == SUCCESS) {
        eui.data[0] = IEEE_EUI64_COMPANY_ID_0;
        eui.data[1] = IEEE_EUI64_COMPANY_ID_1;
        eui.data[2] = IEEE_EUI64_COMPANY_ID_2;

        // 16 bits of the ID is generated by software
        // could be used for hardware model id and revision, for example
        eui.data[3] = IEEE_EUI64_SERIAL_ID_0;
        eui.data[4] = IEEE_EUI64_SERIAL_ID_1;

        // 24 least significant bits of the serial ID read from the DS2411
        eui.data[5] = buf[2];
        eui.data[6] = buf[1];
        eui.data[7] = buf[0];

        have_id = TRUE;
      }
    }
    return eui;
  }
}
